package com.example.test;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;

public class PicturePicker extends AppCompatActivity {
    //调用系统相册-选择图片
    private static final int IMAGE=1;
    //所需权限
    //<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    //<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

//    private ImageView imageView;
//    private static final int CROP_PHOTO = 2;
//    private static final int REEQUEST_CODE_PICK_IMAGE = 3;
//    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 6;
//    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE2 = 7;
//    private File output;
//    private Uri imageUri;
//
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_picker);
//        initView();
    }


    public void choosePhone(View view){
        //调用相册
        Intent intent = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent,IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //获取图片路径
        if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null){
            Uri selectdImage = data.getData();
            String[] filePathColumns = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(selectdImage, filePathColumns,null,null,null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePathColumns[0]);
            String imagePath = c.getString(columnIndex);
            showImage(imagePath);
            c.close();
        }
    }

    //加载图片
    private void showImage(String imagePath){
        Bitmap bm = BitmapFactory.decodeFile(imagePath);
        ((ImageView)findViewById(R.id.image)).setImageBitmap(bm);
    }


}

//
//    void initView() {
//        imageView = (ImageView) findViewById(R.id.image);
//    }
//
//    public void takePhone(View view) {
//        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                != PackageManager.PERMISSION_GRANTED) {
//            ActivityCompat.requestPermissions(this,
//                    new String[]{
//                            Manifest.permission.WRITE_EXTERNAL_STORAGE
//                    }, MY_PERMISSIONS_REQUEST_CALL_PHONE2);
//        } else {
//            takePhone(view);
//        }
//    }
//
//    public void choosePhone(View view) {
//        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                != PackageManager.PERMISSION_GRANTED) {
//            ActivityCompat.requestPermissions(this,
//                    new String[]{
//                            Manifest.permission.WRITE_EXTERNAL_STORAGE
//                    }, MY_PERMISSIONS_REQUEST_CALL_PHONE2);
//        } else {
//            choosePhone(view);
//        }
//    }
//
//    /**
//     * 拍照
//     */
//
//    void takePhone() {
//        /**
//         * 最后一个参数是文件夹的名称，可以随意起名
//         */
//        File file = new File(Environment.getExternalStorageDirectory(), "拍照");
//        if (!file.exists()) {
//            file.mkdir();
//        }
//        /**
//         * 这里将事件作为不同照片的名称
//         */
//        output = new File(file, System.currentTimeMillis() + ".jpg");
//        /**
//         * 如果该文件夹已经存在，则删除它，否则创建一个
//         */
//        try {
//            if (output.exists()) {
//                output.delete();
//            }
//            output.createNewFile();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        /**
//         * 隐式打开拍照的Activity，并且传入CROOP_PHOTO常量作为拍照结束后回调的标志
//         */
//        imageUri = Uri.fromFile(output);
//        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//        startActivityForResult(intent, CROP_PHOTO);
//
//    }
//
//    /**
//     * 从相册选图片
//     */
//    void choosePhoto() {
//        /**
//         * 打开选择图片的界面
//         */
//        Intent intent = new Intent(Intent.ACTION_PICK);
//        intent.setType("image/*");//图片类型
//        startActivityForResult(intent, REEQUEST_CODE_PICK_IMAGE);
//    }
//
//    public void onActivityResult(int req, int res, Intent data) {
//        super.onActivityResult(req, res, data);
//        switch (req) {
//            /**
//             * 拍照的请求标志
//             */
//            case CROP_PHOTO:
//                if (res == RESULT_OK) {
//                    try {
//                        /**
//                         * 该uri就是照片文件夹对应的uri
//                         */
//                        Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
//                        imageView.setImageBitmap(bit);
//                    } catch (Exception e) {
//                        Toast.makeText(this, "程序崩溃", Toast.LENGTH_SHORT).show();
//                    }
//                } else {
//                    Log.i("tag", "失败");
//                }
//                break;
//
//            /**
//             * 从相册中选取图片的请求标志
//             */
//
//            case REEQUEST_CODE_PICK_IMAGE:
//                if (res == RESULT_OK) {
//                    try {
//                        /**
//                         * 该uri是上一个Activity返回的
//                         */
//                        Uri uri = data.getData();
//                        Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
//                        imageView.setImageBitmap(bit);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                        Log.d("tag", e.getMessage());
//                        Toast.makeText(this, "程序崩溃", Toast.LENGTH_SHORT).show();
//                    }
//                } else {
//                    Log.i("liang", "失败");
//                }
//
//                break;
//            default:
//                break;
//        }
//    }
//
//    @Override
//    public void onRequestPermissionResult(int requestCode, String[] permission, int[] grantResults) {
//        if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
//            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                takePhone();
//            } else {
//                //permission Denied
//                Toast.makeText(PicturePicker.this, "Permission Denied", Toast.LENGTH_SHORT).show();
//            }
//        }
//
//        if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE2) {
//            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                choosePhoto();
//            } else {
//                //permission Denied
//                Toast.makeText(PicturePicker.this, "Permission Denied", Toast.LENGTH_SHORT).show();
//            }
//        }
//        super.onRequestPermissionsResult(requestCode, permission, grantResults);
//
//    }



